home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Tool Chest / Graphics & Imaging / Animation ƒ / AnimationTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  11.8 KB  |  608 lines  |  [TEXT/MPS ]

  1. //
  2. // © Copyright 1991 Apple Computer, Inc.  By Ricardo Batista
  3. //
  4. // Sprite Manager test application
  5. //
  6. // 05/16/91
  7. //
  8.  
  9.  
  10. #include <Types.h>
  11. #include <QuickDraw.h>
  12. #include <QDOffscreen.h>
  13. #include <Memory.h>
  14. #include <OSUtils.h>
  15. #include <Windows.h>
  16. #include <Fonts.h>
  17. #include <Menus.h>
  18. #include <TextEdit.h>
  19. #include <Events.h>
  20. #include <Packages.h>
  21. #include <OSEvents.h>
  22. #include <Desk.h>
  23. #include <ToolUtils.h>
  24. #include <Scrap.h>
  25. #include <Resources.h>
  26. #include <Retrace.h>
  27. #include <SysEqu.h>
  28.  
  29. #include "SpriteMgr.h"
  30.  
  31.  
  32. #define        appleMenu            128
  33. #define        fileMenu            129
  34. #define        editMenu            130
  35. #define        setupMenu            131
  36.  
  37.  
  38. #define        quitItem            1
  39.  
  40. #define        undoItem            1
  41. #define        cutItem                3
  42. #define        copyItem            4
  43. #define        pasteItem            5
  44. #define        clearItem            6
  45.  
  46. #define        kCopyGItem            1
  47. #define        kCatItem            2
  48. #define        kHoleItem            3
  49. #define        kSkyItem            4
  50. #define        kWindowItem            5
  51. #define        kClockItem            6
  52.  
  53. #define        okItem                1
  54. #define        cancelItem            2
  55.  
  56.  
  57.  
  58. short Initialize(void);
  59. void SetMenus(void);
  60. void AnalizeKeys(void);
  61. Boolean Working(void);
  62. Boolean DoCommand(long mResult);
  63. void DoAbout();
  64. Boolean HandleMouseDowns(void);
  65. void CloseUp(void);
  66. void UpdateDisplayWindow(void);
  67. void UpdateAnimation(void);
  68.  
  69. EventRecord myEvent;
  70. MenuHandle myMenu[5];
  71. WindowPtr displayWindow;
  72. Boolean shiftK, commandK, optionK;
  73. Boolean inFront;
  74. Boolean activeMenus;
  75. Boolean cursorInvisible;
  76.  
  77. short yBackID[4];
  78. short rBackID[5];
  79. short clockID;
  80. short windowID;
  81. short skyID;
  82. short catID;
  83. Point catPoint;
  84. short mouseHoleID;
  85. Point mouseHolePoint;
  86. Boolean doingCat, doingHole, hasWindow, hasSky, hasClock;
  87.  
  88. main()
  89. {
  90.     if (Initialize())
  91.         return;
  92.     while (Working())
  93.         ;
  94.     CloseUp();
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. void UpdateDisplayWindow(void)
  108. {
  109.     GrafPtr savePort;
  110.     
  111.     GetPort(&savePort);
  112.     SetPort(displayWindow);
  113.     BeginUpdate(displayWindow);
  114.     DoAnimation(true);
  115.     EndUpdate(displayWindow);
  116.     SetPort(savePort);
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. void CloseUp(void)
  124. {
  125.     if (cursorInvisible)
  126.         ShowCursor();
  127.     CloseSpriteMgr();
  128.     displayWindow = 0L;
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. short Initialize(void)
  136. {
  137.     register short counter;
  138.     Rect box;
  139.     short err;
  140.     PicHandle picH;
  141.     Rect bYellowRect;
  142.     Rect bRedBlueFloorRect;
  143.     
  144.     for (counter = 0; counter < 10; counter++)
  145.         MoreMasters();
  146.     InitGraf(&qd.thePort);
  147.     InitFonts();
  148.     InitWindows();
  149.     TEInit();
  150.     InitMenus();
  151.     InitDialogs(0L);
  152.     DrawMenuBar();
  153.     FlushEvents(everyEvent,0L);
  154.     InitCursor();
  155.     MaxApplZone();
  156.     InitAllPacks();
  157.     SetMenus();
  158.     activeMenus = true;
  159.     cursorInvisible = false;
  160.     box.top = box.left = box.right = box.bottom = 0;
  161.     picH = GetPicture(141);        // yellow background
  162.     if (picH) {
  163.         LoadResource((Handle) picH);
  164.         bYellowRect = (**picH).picFrame;
  165.     }
  166.     picH = GetPicture(143);        // red and blue tiles for floor
  167.     if (picH) {
  168.         LoadResource((Handle) picH);
  169.         bRedBlueFloorRect = (**picH).picFrame;
  170.     }
  171.     box.bottom = bYellowRect.bottom + bRedBlueFloorRect.bottom;
  172.     box.right = bYellowRect.right * 4;
  173.     err = InitSpriteMgr(0L);
  174.     if (err) {
  175.         CloseSpriteMgr();
  176.         return(err);
  177.     }
  178.     displayWindow = (WindowPtr) NewAnimation(&box, box.right, box.bottom, "SpriteMgr Test");
  179.     if (!displayWindow) {
  180.         CloseSpriteMgr();
  181.         return(1);
  182.     }
  183.     SetPort(displayWindow);
  184.     box = bYellowRect;
  185.     for (counter = 0; counter < 4; counter++) {
  186.         yBackID[counter] = NewBackground(141, counter + 1, srcCopy, &box, 141 - counter);
  187.         box.left += bYellowRect.right;
  188.         box.right += bYellowRect.right;
  189.     }
  190.     box = bRedBlueFloorRect;
  191.     box.top += bYellowRect.bottom;
  192.     box.bottom += bYellowRect.bottom;
  193.     for (counter = 0; counter < 5; counter++) {
  194.         rBackID[counter] = NewBackground(143, counter + 20, srcCopy, &box, 143 + counter);
  195.         box.left += bRedBlueFloorRect.right;
  196.         box.right += bRedBlueFloorRect.right;
  197.     }
  198.     
  199.     
  200.     
  201.     catID = NewSprite(140, 1, transparent, 0L, 0L, 140, 0L, false, 140);
  202.     catPoint.h = 10;
  203.     catPoint.v = 200;
  204.     PutSprite(catID, catPoint.v, catPoint.h);
  205.     
  206.     mouseHoleID = NewForegroundSprite(134, 1, transparent, 0L, 0L, 134, 0L, false, 134);
  207.     mouseHolePoint.v = 270;
  208.     mouseHolePoint.h = 5;
  209.     PutSprite(mouseHoleID, mouseHolePoint.v, mouseHolePoint.h);
  210.     
  211.     ActivateAnimation();
  212.     DoAnimation(false);        // to show initial screen
  213.     doingCat = false;
  214.     doingHole = true;
  215.     CheckItem(myMenu[setupMenu - 128], kCatItem, doingCat);
  216.     CheckItem(myMenu[setupMenu - 128], kHoleItem, doingHole);
  217.     hasWindow = hasSky = hasClock = false;
  218.     return(0);
  219. }
  220.  
  221.  
  222.  
  223.  
  224. void SetMenus()
  225. {
  226.     char appleStr[2];
  227.     
  228.     appleStr[0] = 1;
  229.     appleStr[1] = 0x14;
  230.     myMenu[appleMenu - 128] = NewMenu(appleMenu,appleStr);
  231.     myMenu[fileMenu - 128] = NewMenu(fileMenu,"\pFile");
  232.     myMenu[editMenu - 128] = NewMenu(editMenu,"\pEdit");
  233.     myMenu[setupMenu - 128] = NewMenu(setupMenu,"\pTest");
  234.     
  235.     AppendMenu(myMenu[appleMenu - 128],"\pAbout Color Sprites...;(-");
  236.     AddResMenu(myMenu[appleMenu - 128],'DRVR');
  237.     
  238.     AppendMenu(myMenu[fileMenu - 128],"\pQuit/Q");
  239.     AppendMenu(myMenu[editMenu - 128],"\pUndo;(-;Cut/X;Copy/C;Paste/V;Clear");
  240.     AppendMenu(myMenu[setupMenu - 128],"\pCopy a GWorld to screen, d6=id;Move Cat;Move MouseHole");
  241.     AppendMenu(myMenu[setupMenu - 128],"\pSky;Window;Clock;(-;optionKey = selective update;commandKey = suspend animation");
  242.     InsertMenu(myMenu[appleMenu - 128],0);
  243.     InsertMenu(myMenu[fileMenu - 128],0);
  244.     InsertMenu(myMenu[editMenu - 128],0);
  245.     InsertMenu(myMenu[setupMenu - 128],0);
  246.     DrawMenuBar();
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253. void AnalizeKeys()
  254. {
  255.     if (myEvent.modifiers & cmdKey)
  256.         commandK = true;
  257.     else
  258.         commandK = false;
  259.     if (myEvent.modifiers & shiftKey)
  260.         shiftK = true;
  261.     else
  262.         shiftK = false;
  263.     if (myEvent.modifiers & optionKey)
  264.         optionK = true;
  265.     else
  266.         optionK = false;
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275. Boolean Working()
  276. {
  277.     register char theChar;
  278.     register Boolean event;
  279.     WindowPtr w;
  280.     KeyMap keys;
  281.     
  282.     GetKeys(&keys[0]);
  283.     if (keys[3] & 0x8) {
  284.         // left
  285.         if (doingCat) {
  286.             catPoint.h -= 8;
  287.             MoveSpriteTo(catID, catPoint.h, catPoint.v, 0);
  288.         }
  289.         if (doingHole) {
  290.             mouseHolePoint.h -= 4;
  291.             MoveSpriteTo(mouseHoleID, mouseHolePoint.h, mouseHolePoint.v, 0);
  292.         }
  293.     }
  294.     if (keys[3] & 0x20) {
  295.         // down
  296.         if (doingCat) {
  297.             catPoint.v += 8;
  298.             MoveSpriteTo(catID, catPoint.h, catPoint.v, 0);
  299.         }
  300.         if (doingHole) {
  301.             mouseHolePoint.v += 4;
  302.             MoveSpriteTo(mouseHoleID, mouseHolePoint.h, mouseHolePoint.v, 0);
  303.         }
  304.     }
  305.     if (keys[3] & 0x10) {
  306.         // right
  307.         if (doingCat) {
  308.             catPoint.h += 8;
  309.             MoveSpriteTo(catID, catPoint.h, catPoint.v, 0);
  310.         }
  311.         if (doingHole) {
  312.             mouseHolePoint.h += 4;
  313.             MoveSpriteTo(mouseHoleID, mouseHolePoint.h, mouseHolePoint.v, 0);
  314.         }
  315.     }
  316.     if (keys[3] & 0x40) {
  317.         // up
  318.         if (doingCat) {
  319.             catPoint.v -= 8;
  320.             MoveSpriteTo(catID, catPoint.h, catPoint.v, 0);
  321.         }
  322.         if (doingHole) {
  323.             mouseHolePoint.v -= 4;
  324.             MoveSpriteTo(mouseHoleID, mouseHolePoint.h, mouseHolePoint.v, 0);
  325.         }
  326.     }
  327.     if (activeMenus || (keys[1] & 0x8000)) {            // command key down, allow menus
  328.         if (!activeMenus) {
  329.             if (cursorInvisible) {
  330.                 ShowCursor();
  331.                 cursorInvisible = false;
  332.             }
  333.         }
  334.         event = WaitNextEvent(everyEvent, &myEvent, 0L, 0L);
  335.         AnalizeKeys();
  336.     }
  337.     else {
  338.         if (cursorInvisible == false) {
  339.             HideCursor();
  340.             cursorInvisible = true;
  341.         }
  342.         event = false;
  343.         DoAnimation(false);
  344.     }
  345.     if (event) {
  346.         switch (myEvent.what) {
  347.             case app4Evt:
  348.                 if (myEvent.modifiers & 128)
  349.                     inFront = true;
  350.                 else {
  351.                     inFront = false;
  352.                     if (cursorInvisible) {
  353.                         ShowCursor();
  354.                         cursorInvisible = false;
  355.                     }
  356.                 }
  357.                 break;
  358.             case activateEvt:
  359.                 w = (WindowPtr) myEvent.message;
  360.                 if (myEvent.modifiers & activeFlag) {
  361.                 }
  362.                 else {
  363.                 }
  364.                 break;
  365.             case keyDown:
  366.             case autoKey:
  367.                 theChar =  myEvent.message & charCodeMask;
  368.                 if (commandK) {
  369.                     if (!DoCommand(MenuKey(theChar)))
  370.                         return(false);
  371.                 }
  372.                 else {
  373.                     w = FrontWindow();
  374.                 }
  375.                 break;
  376.             case updateEvt:
  377.                 w = (WindowPtr) myEvent.message;
  378.                 if (w == displayWindow) {
  379.                     UpdateDisplayWindow();
  380.                 }
  381.                 break;
  382.             case mouseDown:
  383.                 if (!HandleMouseDowns())
  384.                     return(false);
  385.                 break;
  386.             default:
  387.                 break;
  388.         }
  389.     }
  390.     return(true);
  391. }
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. void DoAbout()
  401. {
  402.     MoveTo(100,100);
  403.     DrawString("\p A color sprite test by Ricardo Batista");
  404. }
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411. Boolean DoCommand(long mResult)
  412. {
  413.     register short theItem;
  414.     char st[250];
  415.     Rect box;
  416.     short h, v;
  417.     PicHandle picH;
  418.     
  419.     theItem = LoWord(mResult);
  420.     switch (HiWord(mResult)) {
  421.         case appleMenu:
  422.                 GetItem(myMenu[0],theItem,st);
  423.                 if (theItem > 2)
  424.                     OpenDeskAcc(st);
  425.                 else
  426.                     DoAbout();
  427.                 break;
  428.         case fileMenu:
  429.                 switch (theItem) {
  430.                     case quitItem:
  431.                         return(false);
  432.                         break;
  433.                     default:
  434.                         break;
  435.                 }
  436.                 break;
  437.         case editMenu:
  438.                 if (!SystemEdit(theItem -1)) {
  439.                     switch (theItem) {
  440.                         case undoItem:
  441.                             break;
  442.                         case cutItem:
  443.                             ZeroScrap();
  444.                             TEToScrap();
  445.                             break;
  446.                         case copyItem:
  447.                             ZeroScrap();
  448.                             TEToScrap();
  449.                             break;
  450.                         case pasteItem:
  451.                             TEFromScrap();
  452.                             break;
  453.                         case clearItem:
  454.                             break;
  455.                         default:
  456.                             break;
  457.                     }
  458.                 }
  459.                 break;
  460.         case setupMenu:
  461.                 if (theItem == kCopyGItem)
  462.                     CopyGToScreen();
  463.                 if (theItem == kCatItem) {
  464.                     if (doingCat)
  465.                         doingCat = false;
  466.                     else
  467.                         doingCat = true;
  468.                     CheckItem(myMenu[setupMenu - 128], kCatItem, doingCat);
  469.                 }
  470.                 if (theItem == kHoleItem) {
  471.                     if (doingHole)
  472.                         doingHole = false;
  473.                     else
  474.                         doingHole = true;
  475.                     CheckItem(myMenu[setupMenu - 128], kHoleItem, doingHole);
  476.                 }
  477.                 if (theItem == kClockItem) {
  478.                     if (hasClock) {
  479.                         hasClock = false;
  480.                         KillSprite(clockID);
  481.                     }
  482.                     else {
  483.                         hasClock = true;
  484.                         clockID = NewBackgroundSprite(200, 4, transparent, 0L, 30L, 200, 0L,
  485.                                             false, 200);
  486.                         PutSprite(clockID, 60, 100);
  487.                     }
  488.                     CheckItem(myMenu[setupMenu - 128], kClockItem, hasClock);
  489.                 }
  490.                 if (theItem == kWindowItem) {
  491.                     if (hasWindow) {
  492.                         hasWindow = false;
  493.                         KillSprite(windowID);
  494.                     }
  495.                     else {
  496.                         hasWindow = true;
  497.                         windowID = NewBackgroundSprite(131, 1, transparent, 0L, 0L, 131, 0L, false, 131);
  498.                         PutSprite(windowID, 80, 260);
  499.                     }
  500.                     CheckItem(myMenu[setupMenu - 128], kWindowItem, hasWindow);
  501.                 }
  502.                 if (theItem == kSkyItem) {
  503.                     if (hasSky) {
  504.                         hasSky = false;
  505.                         KillBackground(skyID);
  506.                     }
  507.                     else {
  508.                         hasSky = true;
  509.                         picH = GetPicture(131);        // need window frame
  510.                         if (picH) {
  511.                             LoadResource((Handle) picH);
  512.                             box = (**picH).picFrame;    // get window rectangle, but needs compensation
  513.                         }
  514.                         h = box.right - box.left;
  515.                         v = box.bottom - box.top;
  516.                         box.top = 80 + 4;
  517.                         box.bottom = 80 + v - 4;
  518.                         box.left = 260 + 6;    // compensate for edges of window
  519.                         box.right = 260 + h - 8;
  520.                         skyID = NewBackground(136, 200, srcCopy, &box, 200);
  521.                         AutoScrollBackground(skyID, 0, 2, 20L);
  522.                     }
  523.                     CheckItem(myMenu[setupMenu - 128], kSkyItem, hasSky);
  524.                 }
  525.                 break;
  526.             default:
  527.                 break;
  528.     }
  529.     HiliteMenu(0);
  530.     return(true);
  531. }
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544. Boolean HandleMouseDowns(void)
  545. {
  546.     WindowPtr whichWindow;
  547.     Rect box;
  548.     long new;
  549.     short v, h;
  550.     
  551.     switch (FindWindow(myEvent.where,&whichWindow)) {
  552.         case inSysWindow:
  553.             SystemClick(&myEvent,whichWindow);
  554.             break;
  555.         case inMenuBar:
  556.             return(DoCommand(MenuSelect(myEvent.where)));
  557.             break;
  558.         case inGrow:
  559.             SetRect(&box,160,100,600,600);
  560.             new = GrowWindow(whichWindow,myEvent.where,&box);
  561.             if (new) {
  562.                 v = HiWord(new);
  563.                 h = LoWord(new);
  564.                 SetPort(whichWindow);
  565.                 SizeWindow(whichWindow,h,v,true);
  566.                 EraseRect(&(whichWindow->portRect));
  567.                 InvalRect(&(whichWindow->portRect));
  568.             }
  569.             break;
  570.         case inGoAway:
  571.             if (TrackGoAway(whichWindow,myEvent.where))
  572.                 return(false);
  573.             break;
  574.         case inDrag:
  575.             if (commandK || (FrontWindow() == whichWindow))
  576.                 DragWindow(whichWindow,myEvent.where,&qd.screenBits.bounds);
  577.             else {
  578.                 SelectWindow(whichWindow);
  579.                 SetPort(whichWindow);
  580.             }
  581.             break;
  582.         case inContent:
  583.              if (whichWindow != FrontWindow()) {
  584.                 SelectWindow(whichWindow);
  585.                 SetPort(whichWindow);
  586.             }
  587.             else {
  588.                 if (whichWindow == displayWindow) {
  589.                     if (activeMenus == false) {
  590.                         DeactivateAnimation();
  591.                         ShowCursor();
  592.                         activeMenus = true;
  593.                         cursorInvisible = false;
  594.                     }
  595.                     else {
  596.                         ActivateAnimation();
  597.                         HideCursor();
  598.                         activeMenus = false;
  599.                         cursorInvisible = true;
  600.                     }
  601.                 }
  602.             }
  603.             break;
  604.         default:
  605.             break;
  606.     }
  607.     return(true);
  608. }